package collection;

import java.util.*;

public class TestMap {


    public static void main(String[] args) {
        Person p1 = new Person("王二麻子", 23);
        Person p2 = new Person("礼拜", 12);
        Person p3 = new Person("老干妈", 45);
        Person p4 = new Person("王致和", 5);

        System.out.println(p1.hashCode());

        p1.setAge(34567);
        System.out.println(p1.hashCode());
        HashMap hashMap = new HashMap();

        Object o = new Object();
        hashMap.put("aa", "b");

        //测试TreeMap
        // TreeMap的key 必须实现compare接口。
        TreeMap<Person, String> treeMap = new TreeMap<>();
        treeMap.put(p1, "你好");
        treeMap.put(p2, "你好");
        treeMap.put(p3, "你好");
        treeMap.put(p4, "你好");
        Set<Map.Entry<Person, String>> treeEntry = treeMap.entrySet();
        for (Map.Entry<Person, String> item : treeEntry) {
            System.out.println(item.getKey() + " -  " + item.getValue());
        }

        TreeMap<Integer, String> treeMap1 = new TreeMap<>();
        treeMap1.put(6, "sdf");
        treeMap1.put(1, "sdffsfg");
        treeMap1.put(3, "sdfdsfsfd");
        for (Map.Entry entry : treeMap1.entrySet()) {
            System.out.println(entry.getKey());
        }


        //测试TreeSet
        TreeSet<Person> treeSet = new TreeSet<>();
        treeSet.add(p1);
        treeSet.add(p2);
        treeSet.add(p3);
        for (Person person : treeSet) {
            System.out.println(person.getAge());

        }

        //测试LinkedHashMap
        System.out.println("***************8测试链表map*************");
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(p1, "你好");
        linkedHashMap.put(p2, "你好");
        linkedHashMap.put(p3, "你好");
        linkedHashMap.put(p4, "你好");
        Set<Map.Entry<Person, String>> lindedEntry = linkedHashMap.entrySet();
        for (Map.Entry<Person, String> item : lindedEntry) {
            System.out.println(item.getKey() + " -  " + item.getValue());
        }
    }
}


/*可以看到使用LinkedHashMap读取的和插入的顺序是一样的*/